/**
 * Created by huanggw on 2015/11/7.
 */
var loginApp = angular.module('login',[]);
loginApp.controller('loginCtrl', function($scope, $timeout) {
    $scope.loginStatus = 0;
    $scope.loginForm = function() {
        var url = host+"/login";
        var obj = {
            username: $scope.username,
            password: $scope.password
        }
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

        xhr.onreadystatechange=function(){
            if(xhr.readyState==4){
                if(xhr.status==200){
                    $timeout(function(){
                        var result = JSON.parse(xhr.responseText)
                        var form = document.getElementById("form");
                        var success = document.getElementById("success");
                        form.style.opacity = 0;
                        success.style.display = 'block';
                        if(result.loginStatus == 0) {
                            $scope.loginTip = "登录失败，"+result.content;
                            $scope.loginStatus = 0;
                            $timeout(function(){
                                window.location.href = '/';
                            }, 3000)
                        }
                        else {
                            $scope.loginTip = "登录成功";
                            $scope.loginStatus = 1;
                            $timeout(function(){
                                window.location.href = '/';
                            }, 2000)
                        }
                        $timeout(function(){
                            form.style.display = 'none'
                            $scope.status = "form-success"
                        }, 300);
                    },20)
                }
                else {

                }
            }
        }
        xhr.send(JSON.stringify(obj));
    }
})